﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HelpCentral.Data.Model;
using HelpCentral.Shared.Interfaces;
using HelpCentral.Configuration;

namespace HelpCentral.Data.Repository
{
    internal static class UserExtensions
    {
        public static IUser FromRepositoryObject(this User source)
        {
            if (source == null) return null;

            var u = CoreContainer.ResolveNew<IUser>();

            u.UserId = source.UserId;
            u.Username = source.Username;
            u.ActivateDate = source.ActivateDate;
            u.DeactivateDate = source.DeactivateDate;
            u.ADUsername = source.ADUsername;
            u.PasswordHash = source.PasswordHash;
            u.PasswordExpirationDate = source.PasswordExpirationDate;

            if (source.Client != null)
            {
                u.Client = source.Client.FromRepositoryObject();
            }

            return u;
        }

        public static User ToRepositoryObject(this IUser source, HelpCentralModelContainer context)
        {
            if (source == null) return null;

            var u = context.User.FirstOrDefault(x => x.UserId == source.UserId);

            if (u == null)
            {
                u = new User();
            }

            u.Username = source.Username;
            u.ActivateDate = source.ActivateDate;
            u.DeactivateDate = source.DeactivateDate;
            u.ADUsername = source.ADUsername;
            u.PasswordHash = source.PasswordHash;
            u.PasswordExpirationDate = source.PasswordExpirationDate;
            u.Client = source.Client.ToRepositoryObject(context);

            return u;
        }
    }
}
